<template>
  <app-section>
    <van-nav-bar :title="navTitle" @click-left="backRoute" left-arrow/>
    <van-pull-refresh v-model="pullLoading" @refresh="onRefresh">
      <van-list
        class="celllist"
        v-model="listLoading"
        :finished="listFinished"
        :finished-text="finishedText"
        :error.sync="listError"
        error-text="请求失败，点击重新加载"
        @load="onLoad()">

        <van-cell
          icon="static/mobile/icon/liucheng.png"
          v-for="(item,index) in applyList"
          :key="index"
          :title="item.bpmBizTitle"
          is-link
          :to="flowDetailRoute(item.processInstanceId,item.endTime)">
          <template slot="label">

            <van-tag v-if="item.endTime&&item.endTime!=''" plain type="success">{{ item.prcocessDefinitionName }}</van-tag>
            <van-tag v-else plain type="primary">{{ item.prcocessDefinitionName }}</van-tag>

            <span style="margin-left:5px">{{ item.startTime }}</span>
          </template>

          <div slot="right-icon">
            <van-icon color="#39ce80" size="0.9rem" v-if="item.bpmStatus == '3' || item.bpmStatus == '已完成'" name="certificate" />
            <span v-else></span>
          </div>
        </van-cell>

      </van-list>
    </van-pull-refresh>
  </app-section>
</template>

<script>
  import AppBlock from '@/components/AppBlock'
  import AppSection from '@/components/AppSection'
  import { getAction } from '@/api'

  export default {
    name: "tab-apply",
    components:{
      AppSection,AppBlock
    },
    data() {
      return {
        navTitle:"我的流程",
        listError:false,
        listLoading: false,
        pullLoading: false,
        listFinished:false,
        finishedText:'没有更多了',
        pageNo:1,
        pageSize:10,
        url:"/act/task/myApplyProcessList",
        applyList:[]
      }
    },
    mounted(){

    },
    watch: {
      '$route' () {
        this.onRefresh()
      }
    },
    methods: {
      flowDetailRoute(processInstanceId,endTime){
        //console.log("processInstanceId",processInstanceId)
        return {
          name: "flowDetail",
          params: {
            instanceId: processInstanceId,
            successed:!endTime?0:1
          }
        }
      },
      onRefresh() {
        this.pullLoading = true
        this.pageNo = 1
        let param = {
          pageNo: this.pageNo,
          pageSize: this.pageSize
        }
        this.applyList=[]
        getAction(this.url,param).then(res=>{
          //console.log('222',res)
          this.pullLoading = false
          if(res.success){
            this.pageNo++
            let arr = res.result.records
            for(let item of arr){
              this.applyList.push(item)
            }
          }
        })
      },
      onLoad(){
        this.listLoading = true;
        let param = {
          pageNo: this.pageNo,
          pageSize: this.pageSize
        }
        getAction(this.url,param).then(res=>{
          this.listLoading = false;
          if(res.success){
           // this.$toast.success("加载完成");
            this.pageNo++
            let arr = res.result.records
            for(let item of arr){
              //console.log(item.bpmStatus)
              this.applyList.push(item)
            }

            if(!arr || arr.length<this.pageSize){
              console.log("加载完成!")
              this.listFinished = true;
            }
            console.log("结果数据", this.applyList)
          }else{
            this.error = true;
          }
        }).catch(e=>{
          console.log("请求错误",e)
          this.error = true;
        })
      },
        backRoute(){
            this.$router.go(-1);//返回上一层
        },

    },

  }
</script>

<style scoped lang="less">
  .demo-pull-refresh {
    background-color:#fff;

    .van-pull-refresh {
      &,
      &__track {
        height: calc(100vh - 50px);
      }
    }

  }
  .celllist{
    .van-cell__label {
      margin-bottom: 3px;
    }
  }
</style>
